package com.haojiang.mh.remote.ui.frag

import android.os.Message
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import cn.com.heaton.blelibrary.ble.utils.ByteUtils
import cn.com.heaton.blelibrary.ble.utils.ThreadUtils
import com.google.android.material.tabs.TabLayout
import com.haojiang.mh.AppContext
import com.haojiang.mh.R
import com.haojiang.mh.remote.base.BaseFrag
import com.haojiang.mh.remote.bean.AreaValue
import com.haojiang.mh.remote.enums.*
import com.haojiang.mh.remote.event.BedImgEvent
import com.haojiang.mh.remote.event.RxMotorEvent
import com.haojiang.mh.remote.event.RxMotorLiveEvent
import com.haojiang.mh.remote.utils.BleUtil
import com.haojiang.mh.remote.utils.BleUtil.Companion.calcLastByte
import com.haojiang.mh.remote.utils.BleUtil.Companion.inRangeOfView
import com.haojiang.mh.remote.utils.BleUtil.Companion.onActionDown
import com.haojiang.mh.remote.utils.BleUtil.Companion.onActionUp
import com.haojiang.mh.remote.utils.CmdKey
import com.haojiang.mh.remote.utils.CmdKey.FOOT_DOWN
import com.haojiang.mh.remote.utils.CmdKey.FOOT_UP
import com.haojiang.mh.remote.utils.CmdKey.HEAD_DOWN
import com.haojiang.mh.remote.utils.CmdKey.HEAD_FOOT_BOTH_DOWN
import com.haojiang.mh.remote.utils.CmdKey.HEAD_FOOT_BOTH_UP
import com.haojiang.mh.remote.utils.CmdKey.HEAD_UP
import com.haojiang.mh.remote.utils.CmdKey.HEAD_UP_FOOT_UP
import com.haojiang.mh.remote.utils.CmdKey.LUMBAR_DOWN
import com.haojiang.mh.remote.utils.CmdKey.LUMBAR_UP
import com.haojiang.mh.remote.utils.CmdKey.PILLOW_DOWN
import com.haojiang.mh.remote.utils.CmdKey.PILLOW_LUMBAR_BOTH_DOWN
import com.haojiang.mh.remote.utils.CmdKey.PILLOW_LUMBAR_BOTH_UP
import com.haojiang.mh.remote.utils.CmdKey.PILLOW_UP
import com.haojiang.mh.remote.utils.LogUtil
import com.haojiang.mh.remote.utils.UuidUtil
import com.haojiang.mh.remote.widget.RmText
import com.king.view.arcseekbar.ArcSeekBar
import kotlinx.android.synthetic.main.frag_motor.ivBed
import kotlinx.android.synthetic.main.frag_motor.tabLayout
import kotlinx.android.synthetic.main.frag_motor_new.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.jetbrains.anko.textColor


class MotorNewFrag : BaseFrag() {


    private var rxMotorList: ArrayList<AreaValue>? = null
    private var degreeViewMap: MutableMap<String, TextView>? = null
    private var degreeUnit: String? = null
    private var goldenColor: Int = 0
    private var whiteColor: Int = 0

    //按键数据回调
    private val onNewTouchListener = object : View.OnTouchListener {

        override fun onTouch(view: View?, event: MotionEvent?): Boolean {
            if (event != null && view != null) {
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        onNewActionDown(view)
                    }

                    MotionEvent.ACTION_MOVE -> {
                        if (inRangeOfView(view, event.rawX.toInt(), event.rawY.toInt())) {
                            onNewActionUp(view)
                        }
                    }

                    MotionEvent.ACTION_CANCEL,
                    MotionEvent.ACTION_UP -> {
                        onNewActionUp(view)
                    }
                }
            }
            return false
        }
    }

    //按键按下
    private fun onNewActionDown(view: View) {
        LogUtil.e("onNewActionDown")
        if (BleUtil.ble.isBleEnable && UuidUtil.curUuidMap.size > 0) {
            BleUtil.frontHandler.postDelayed(Runnable {
                view.tag?.let {
                    val msg = Message.obtain()
                    msg.what = EPressType.KEEP.key      //按下类型-持续发送
                    val motorByte = ByteUtils.hexStr2Bytes(getCurMotorCode(view))[0]
                    msg.obj = calcLastByte(byteArrayOf(0x6d, 0x06, 0x01, 0x01, motorByte, 0))       //持续发送
                    BleUtil.sendHandler.handleMessage(msg)
                }
            }, 0)
        }
    }

    //按键抬起
    private fun onNewActionUp(view: View) {
        LogUtil.e("onNewActionUp")
        BleUtil.frontHandler.removeCallbacksAndMessages(null)
        BleUtil.sendHandler.removeCallbacksAndMessages(null)
    }

    //获取当前马达的byte值
    private fun getCurMotor(): String {
        return tabLayout.getTabAt(tabLayout.selectedTabPosition)!!.tag as String
    }

    //获取当前马达的byte值
    private fun getCurMotorByte(): Byte {
        return ByteUtils.hexStr2Bytes(getCurMotor())[0]
    }

    private fun getCurMotorCode(view: View): String {
        view.tag?.let {
            val motorCode = getCurMotor()
            val motorDir = view.tag as String
            when (motorCode) {
                EMotor.HEAD.value -> {
                    if (motorDir.equals(EMotorBtn.UP.value)) {
                        return AppContext.instance().getString(R.string.head_up)
                    } else {
                        return AppContext.instance().getString(R.string.head_down)
                    }
                }
                EMotor.FOOT.value -> {
                    if (motorDir.equals(EMotorBtn.UP.value)) {
                        return AppContext.instance().getString(R.string.foot_up)
                    } else {
                        return AppContext.instance().getString(R.string.foot_down)
                    }
                }
                EMotor.PILLOW.value -> {
                    if (motorDir.equals(EMotorBtn.UP.value)) {
                        return AppContext.instance().getString(R.string.pillow_up)
                    } else {
                        return AppContext.instance().getString(R.string.pillow_down)
                    }
                }
                EMotor.LUMBAR.value -> {
                    if (motorDir.equals(EMotorBtn.UP.value)) {
                        return AppContext.instance().getString(R.string.lumbar_up)
                    } else {
                        return AppContext.instance().getString(R.string.lumbar_down)
                    }
                }
                EMotor.MOTOR5.value -> {
                    if (motorDir.equals(EMotorBtn.UP.value)) {
                        return AppContext.instance().getString(R.string.motor5_up)
                    } else {
                        return AppContext.instance().getString(R.string.motor5_down)
                    }
                }
                EMotor.MOTOR6.value -> {
                    if (motorDir.equals(EMotorBtn.UP.value)) {
                        return AppContext.instance().getString(R.string.motor6_up)
                    } else {
                        return AppContext.instance().getString(R.string.motor6_down)
                    }
                }

                EMotor.HEAD_FOOT_BOTH.value -> {
                    if (motorDir.equals(EMotorBtn.UP.value)) {
                        return AppContext.instance().getString(R.string.head_foot_both_up)
                    } else {
                        return AppContext.instance().getString(R.string.head_foot_both_down)
                    }
                }
                EMotor.PILLOW_LUMBAR_BOTH.value -> {
                    if (motorDir.equals(EMotorBtn.UP.value)) {
                        return AppContext.instance().getString(R.string.pillow_lumbar_both_up)
                    } else {
                        return AppContext.instance().getString(R.string.pillow_lumbar_both_down)
                    }
                }
                EMotor.PILLOW_LUMBAR_BOTH_TILT.value -> {
                    if (motorDir.equals(EMotorBtn.UP.value)) {
                        return AppContext.instance().getString(R.string.pillow_lumbar_both_tilt_up)
                    } else {
                        return AppContext.instance().getString(R.string.pillow_lumbar_both_tilt_down)
                    }
                }
//                EMotor.HEAD_UP_FOOT_UP.value -> {
//                    if (motorDir.equals(EMotorBtn.UP.value)) {
//                        return AppContext.instance().getString(R.string.head_up_foot_up)
//                    }
//                }
//                EMotor.HEAD_UP_FOOT_DOWN.value -> {
//                    if (motorDir.equals(EMotorBtn.UP.value)) {
//                        return AppContext.instance().getString(R.string.head_up_foot_down)
//                    }
//                }
//                EMotor.HEAD_DOWN_FOOT_UP.value -> {
//                    if (motorDir.equals(EMotorBtn.UP.value)) {
//                        return AppContext.instance().getString(R.string.head_down_foot_up)
//                    }
//                }
//                EMotor.HEAD_UP_ONLY.value -> {
//                    if (motorDir.equals(EMotorBtn.UP.value)) {
//                        return AppContext.instance().getString(R.string.head_up_only)
//                    }
//                }
//                EMotor.FOOT_UP_ONLY.value -> {
//                    if (motorDir.equals(EMotorBtn.UP.value)) {
//                        return AppContext.instance().getString(R.string.foot_up_only)
//                    }
//                }
                else -> {
                }
            }
        }
        return ""
    }

    override fun onLayoutId(): Int {
        return R.layout.frag_motor_new
    }

    override fun initView() {
//        LogUtil.e("马达视图被加载")
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
    }

//    private val onClickListener = object : View.OnClickListener {
//        override fun onClick(v: View?) {
//            when (v!!.id) {
//                R.id.ivPlus -> {
//                    sendMotorData(EPressType.KEEP)
////                    arcSeekBar.progress = arcSeekBar.progress + 1
//                }
//                R.id.ivMinus -> {
//                    sendMotorData(EPressType.KEEP)
////                    arcSeekBar.progress = arcSeekBar.progress - 1
//                }
//            }
//        }
//    }

    override fun initListener() {
//        LogUtil.e("马达被加载")
        degreeUnit = AppContext.instance().getString(R.string.f_degree)
        whiteColor = ContextCompat.getColor(context, R.color.color_white)
        goldenColor = ContextCompat.getColor(context, R.color.color_golden_light)
        tabLayout.addOnTabSelectedListener(onTabSelectedListener)

        arcSeekBar.setProgressColor(goldenColor, goldenColor, goldenColor, goldenColor, goldenColor, goldenColor)
        arcSeekBar.setOnChangeListener(object : ArcSeekBar.OnChangeListener {
            override fun onStartTrackingTouch(isCanDrag: Boolean) {
            }

            override fun onProgressChanged(progress: Float, max: Float, fromUser: Boolean) {
//                Log.e("colin", "$progress===$max")
                arcSeekBar.setLabelText(progress.toInt().toString() + degreeUnit)
            }

            override fun onStopTrackingTouch(isCanDrag: Boolean) {
//                setTabDegree()
                LogUtil.e("onStopTrackingTouch")
                sendMotorData()
            }

            override fun onSingleTapUp() {
                sendMotorData()
                LogUtil.e("onSingleTapUp")
            }
        })


//        ivPlus.setOnClickListener(onClickListener)
//        ivMinus.setOnClickListener(onClickListener)
        ivPlus.setOnTouchListener(onNewTouchListener)
        ivMinus.setOnTouchListener(onNewTouchListener)
    }

    override fun initData() {
//        showFrag()
//        showFunction()
    }

    private fun setTabDegree() {
        rxMotorList?.let {
            val areaValue = rxMotorList!!.get(tabLayout.selectedTabPosition)
            areaValue.curValue = arcSeekBar.progress
            val tvTitle = degreeViewMap!!.get(areaValue.code)
            tvTitle!!.setText(arcSeekBar.progress.toString() + degreeUnit)
        }
    }


    //自定义图标字体和文字
//    fun getTabView(pos: Int): View {
//        //首先为子tab布置一个布局
//        val view: View = LayoutInflater.from(context).inflate(R.layout.layout_tab, null)
//        val tvTitle: TextView = view.findViewById(R.id.tvTitle)
//        val ivIcon: ImageView = view.findViewById(R.id.ivIcon);
//        tvTitle.setText(motorMap.get(pos));
//        tvTitle.setText(motorMap.get(pos));
//        ivIcon.setImageResource(tabIcons.get(pos));
//        return view;
//    }


    override fun initDestroy() {
        EventBus.getDefault().unregister(this)
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onRxMotorEvent(event: RxMotorEvent) {
        rxMotorList = event.rxMotorLIst
        rxMotorList?.let {
            handleMotorInit()           //初始化马达数据
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onRxMotorLiveEvent(event: RxMotorLiveEvent) {
        handleMotorLive(event.motor, event.curValue)
    }

    private fun handleMotorLive(motor: String, curValue: Int) {
        val tab = tabLayout.getTabAt(tabLayout.selectedTabPosition)
        tab?.let {
            val code = tab.tag as String
            LogUtil.e(code + "=" + motor)
            if (code.equals(motor, ignoreCase = true)) {
                arcSeekBar.progress = curValue
            }
            degreeViewMap!!.get(motor)!!.text = curValue.toString() + degreeUnit
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onBedImgEvent(event: BedImgEvent) {
        setBedImg(event.bedStr)
    }

    @Synchronized
    private fun handleMotorInit() {
        //TODO 先过滤是否为单按
        LogUtil.e(rxMotorList!!.toString())
        setTabData()
    }


    val onTabSelectedListener = object : TabLayout.OnTabSelectedListener {
        override fun onTabReselected(tab: TabLayout.Tab?) {

        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {

        }

        override fun onTabSelected(tab: TabLayout.Tab?) {
            getMotorData(tab!!.tag.toString())
        }
    }

    private fun getMotorData(motor: String) {
        rxMotorList?.let {
            rxMotorList!!.forEach {

                if (it.code.equals(motor, ignoreCase = true)) {
                    arcSeekBar.max = it.rangeMap!!.get(ERange.MAX)!!
                    arcSeekBar.progress = it.curValue
                    degreeViewMap?.let {
                        degreeViewMap!!.forEach {
                            val tvTitle = it.value
                            tvTitle.setTextColor(whiteColor)
                        }
                        val tvTitle = degreeViewMap!!.get(motor)
                        tvTitle?.let {
                            tvTitle.setTextColor(goldenColor)
                        }
                    }
                }
            }
        }
    }


    private fun sendMotorData() {
        if (BleUtil.ble.isBleEnable && UuidUtil.curUuidMap.size > 0) {
            val motorByte = getCurMotorByte()
            val processByte = arcSeekBar.progress.toByte()
            BleUtil.frontHandler.postDelayed(Runnable {
                val msg = Message.obtain()
                msg.what = EPressType.ONCE.key
                msg.obj = calcLastByte(byteArrayOf(0x6D, 0x06, 0x01, 0x02, motorByte, processByte, 0))
                BleUtil.sendHandler.handleMessage(msg)
            }, 0)
        }

//        view.img?.let {
//            EventBus.getDefault().post(BedImgEvent(it))
//        }
    }

    fun addTab(ItemCode: String, itemTitle: String, itemRange: Map<ERange, Int>, itemIconRes: Int) {
        val tab = tabLayout.newTab()
        val view: View = LayoutInflater.from(context).inflate(R.layout.layout_tab_new, null)
        val tvTitle: TextView = view.findViewById(R.id.tvTitle)
        val ivIcon: ImageView = view.findViewById(R.id.ivIcon)
        tvTitle.setText(itemTitle + degreeUnit!!)
        if (tabLayout.tabCount == 0) {
            tvTitle.textColor = goldenColor
        }
        ivIcon.setImageResource(itemIconRes)
        tab.setCustomView(view)
        tab.tag = ItemCode
        tabLayout.addTab(tab)
        degreeViewMap!!.put(ItemCode, tvTitle)
    }


    fun setTabData() {
        if (degreeViewMap == null) {
            degreeViewMap = LinkedHashMap()
        } else {
            degreeViewMap!!.clear()
        }
        rxMotorList?.let {
            ThreadUtils.ui {
                tabLayout.removeAllTabs()
                if (rxMotorList!!.size > 0) {
                    val motorData = rxMotorList!!.get(0)
                    val curValue = motorData.curValue
                    val max = motorData.rangeMap!!.get(ERange.MAX)!!
                    arcSeekBar.labelText = curValue.toString() + degreeUnit
                    arcSeekBar.progress = curValue
                    arcSeekBar.max = max
                }
                rxMotorList!!.forEach {
                    when (it.code) {
                        EMotor.HEAD.value -> {
                            addTab(EMotor.HEAD.value, it.curValue.toString(), it.rangeMap!!, R.drawable.ic_head_n)
                        }
                        EMotor.FOOT.value -> {
                            addTab(EMotor.FOOT.value, it.curValue.toString(), it.rangeMap!!, R.drawable.ic_foot_n)
                        }
                        EMotor.PILLOW.value -> {
                            addTab(EMotor.PILLOW.value, it.curValue.toString(), it.rangeMap!!, R.drawable.ic_pillow_n)
                        }
                        EMotor.LUMBAR.value -> {
                            addTab(EMotor.LUMBAR.value, it.curValue.toString(), it.rangeMap!!, R.drawable.ic_lumbar)
                        }
                        EMotor.MOTOR5.value -> {
                            addTab(EMotor.MOTOR5.value, it.curValue.toString(), it.rangeMap!!, R.drawable.ic_motor5)
                        }
                        EMotor.MOTOR6.value -> {
                            addTab(EMotor.MOTOR6.value, it.curValue.toString(), it.rangeMap!!, R.drawable.ic_motor6)
                        }

                        EMotor.HEAD_FOOT_BOTH.value -> {
                            addTab(EMotor.HEAD_FOOT_BOTH.value, it.curValue.toString(), it.rangeMap!!, R.drawable.ic_head_foot_both)
                        }
                        EMotor.PILLOW_LUMBAR_BOTH.value -> {
                            addTab(EMotor.PILLOW_LUMBAR_BOTH.value, it.curValue.toString(), it.rangeMap!!, R.drawable.ic_pillow_lumbar_both)
                        }
                        EMotor.PILLOW_LUMBAR_BOTH_TILT.value -> {
                            addTab(
                                EMotor.PILLOW_LUMBAR_BOTH_TILT.value,
                                it.curValue.toString(),
                                it.rangeMap!!,
                                R.drawable.ic_pillow_lumbar_both_tilt
                            )
                        }
//                        EMotor.HEAD_UP_FOOT_UP.value -> {
//                            addTab(EMotor.HEAD_UP_FOOT_UP.value, it.curValue.toString(), it.rangeMap!!, R.drawable.ic_head_up_foot_up)
//                        }
//                        EMotor.HEAD_UP_FOOT_DOWN.value -> {
//                            addTab(EMotor.HEAD_UP_FOOT_DOWN.value, it.curValue.toString(), it.rangeMap!!, R.drawable.ic_head_up_foot_down)
//                        }
//                        EMotor.HEAD_DOWN_FOOT_UP.value -> {
//                            addTab(EMotor.HEAD_DOWN_FOOT_UP.value, it.curValue.toString(), it.rangeMap!!, R.drawable.ic_head_down_foot_up)
//                        }
//                        EMotor.HEAD_UP_ONLY.value -> {
//                            addTab(EMotor.HEAD_UP_ONLY.value, it.curValue.toString(), it.rangeMap!!, R.drawable.ic_head_up_only_n)
//                        }
//                        EMotor.FOOT_UP_ONLY.value -> {
//                            addTab(EMotor.FOOT_UP_ONLY.value, it.curValue.toString(), it.rangeMap!!, R.drawable.ic_foot_up_only_n)
//                        }
                    }

                }
            }
        }
    }


    val onMotorTouchListener = object : View.OnTouchListener {

        override fun onTouch(view: View?, event: MotionEvent?): Boolean {
            if (event != null && view != null && view is RmText) {

                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        val tab = tabLayout?.getTabAt(tabLayout!!.selectedTabPosition)
                        val itemViewCode = tab?.tag.toString()
                        val viewTag = view.tag
                        when (itemViewCode.toInt()) {
                            EMotor.HEAD.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = HEAD_UP
                                        view.img = context.getString(R.string.bed_head)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = HEAD_DOWN
                                        view.img = context.getString(R.string.bed_head)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.FOOT.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = FOOT_UP
                                        view.img = context.getString(R.string.bed_foot)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = FOOT_DOWN
                                        view.img = context.getString(R.string.bed_foot)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.PILLOW.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = PILLOW_UP
                                        view.img = context.getString(R.string.bed_pillow)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = PILLOW_DOWN
                                        view.img = context.getString(R.string.bed_pillow)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.LUMBAR.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = LUMBAR_UP
                                        view.img = context.getString(R.string.bed_lumbar)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = LUMBAR_DOWN
                                        view.img = context.getString(R.string.bed_lumbar)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.MOTOR5.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = CmdKey.MOTOR5_UP
                                        view.img = context.getString(R.string.bed_all)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = CmdKey.MOTOR5_DOWN
                                        view.img = context.getString(R.string.bed_all)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.MOTOR6.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = CmdKey.MOTOR6_UP
                                        view.img = context.getString(R.string.bed_all)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = CmdKey.MOTOR6_DOWN
                                        view.img = context.getString(R.string.bed_all)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.HEAD_FOOT_BOTH.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = HEAD_FOOT_BOTH_UP
                                        view.img = context.getString(R.string.bed_head_foot_both)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = HEAD_FOOT_BOTH_DOWN
                                        view.img = context.getString(R.string.bed_head_foot_both)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

                            EMotor.PILLOW_LUMBAR_BOTH.key -> {
                                when (viewTag) {
                                    EMotorBtn.UP.value -> {
                                        view.code = PILLOW_LUMBAR_BOTH_UP
                                        view.img = context.getString(R.string.bed_pillow_lumbar_both)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                    EMotorBtn.DOWN.value -> {
                                        view.code = PILLOW_LUMBAR_BOTH_DOWN
                                        view.img = context.getString(R.string.bed_pillow_lumbar_both)
                                        view.pressType = context.getString(R.string.press_keep)
                                        onActionDown(view)
                                    }
                                }
                            }

//                            EMotor.HEAD_UP_FOOT_UP.key -> {
//                                when (viewTag) {
//                                    EMotorBtn.UP.value -> {
//                                        view.code = HEAD_UP_FOOT_UP
//                                        view.img = context.getString(R.string.bed_all)
//                                        view.pressType = context.getString(R.string.press_once)
//                                        onActionDown(view)
//                                    }
//                                }
//                            }
//
//                            EMotor.HEAD_UP_FOOT_DOWN.key -> {
//                                when (viewTag) {
//                                    EMotorBtn.UP.value -> {
//                                        view.code = CmdKey.HEAD_UP_FOOT_DOWN
//                                        view.img = context.getString(R.string.bed_all)
//                                        view.pressType = context.getString(R.string.press_once)
//                                        onActionDown(view)
//                                    }
//                                }
//                            }
//
//                            EMotor.HEAD_DOWN_FOOT_UP.key -> {
//                                when (viewTag) {
//                                    EMotorBtn.UP.value -> {
//                                        view.code = CmdKey.HEAD_DOWN_FOOT_UP
//                                        view.img = context.getString(R.string.bed_all)
//                                        view.pressType = context.getString(R.string.press_once)
//                                        onActionDown(view)
//                                    }
//                                }
//                            }

                        }
                    }

                    MotionEvent.ACTION_MOVE -> {
                        if (inRangeOfView(view, event.rawX.toInt(), event.rawY.toInt())) {
                            onActionUp(view)
                        }
                    }

                    MotionEvent.ACTION_CANCEL,
                    MotionEvent.ACTION_UP -> {
                        onActionUp(view)
                    }
                }
            }
            return false
        }
    }


    private fun setBedImg(bedStr: String) {
        when (bedStr) {
            context.getString(R.string.bed_none) -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_none)
            }
            context.getString(R.string.bed_head) -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_head)
            }
            context.getString(R.string.bed_foot) -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_foot)
            }
            context.getString(R.string.bed_pillow)
            -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_pillow)
            }
            context.getString(R.string.bed_lumbar) -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_lumbar)
            }
            context.getString(R.string.bed_head_foot_both) -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_head_foot_both)
            }
            context.getString(R.string.bed_pillow_lumbar_both)
            -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_all)
            }
            context.getString(R.string.bed_all)
            -> {
                ivBed!!.setImageResource(R.drawable.ic_bed_all)
            }
            context.getString(R.string.bed_ubl) -> {
                if (ivBed!!.tag == null) {
                    ivBed!!.setImageResource(R.drawable.ic_bed_ubl)
                    ivBed!!.tag = R.drawable.ic_bed_ubl
                } else {
                    ivBed!!.setImageResource(R.drawable.ic_bed_none)
                    ivBed!!.tag = null
                }
            }
        }
    }

}